package cn.mall.product.service.impl;

import cn.mall.common.utils.PageUtils;
import cn.mall.common.utils.Query;
import cn.mall.product.dao.AttrGroupDao;
import cn.mall.product.entity.AttrGroupEntity;
import cn.mall.product.service.AttrGroupService;
import cn.mall.product.vo.AttrGroupVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    private AttrGroupDao groupDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageBylist(Map<String, Object> params, Long catelogId) {
            String key = (String) params.get("key");
            QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>();
            if (!StringUtils.isEmpty(key)) {
                queryWrapper.and(obj -> {
                    obj.eq("attr_group_id", key).or().like("attr_group_name", key);
                });
            }

            //如果三级分类id为0，则查询所有属性分组
            if (catelogId ==0) {
                IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),queryWrapper);
                return new PageUtils(page);
            } else {
                queryWrapper.eq("catelog_id", catelogId);
                IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),queryWrapper);
                return new PageUtils(page);
            }
    }

    @Override
    public List<AttrGroupVo> getAttrGroupWithAttrBySkuId(Long spuId, Long catelogId) {
        return groupDao.attrGroupBySpuById(spuId, catelogId);
    }

}